package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap;

import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.common.OtwConstant;
import com.alibaba.cirtus.ots.application.adapter.ability.model.Result;
import com.alibaba.cirtus.ots.application.adapter.ability.model.enums.MnSAPStatus;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpTokenUtil;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpUtils;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import com.epoch.app.otsapplicationadaptersdk.mnsapif021v2.dto.CallBackRequest;
import com.epoch.app.otsapplicationadaptersdk.mnsapif021v2.service.MnSapIf021V2Service;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommResponse;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommSDO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MnSapIf021V2ServiceImpl implements MnSapIf021V2Service {

    private final Log log = Log.getLogger(MnSapIf021V2ServiceImpl.class);

    @Autowired
    private HttpUtils httpUtils;

    /**
     * callBack
     *
     * @param callBackRequest
     */
    @Override
    public MnSapCommResponse callBack(CallBackRequest callBackRequest) {
        log.info("MnSapIf021V2ServiceImpl callBack param:{}", JSON.toJSONString(callBackRequest));
        MnSapCommResponse mnSapCommResponse = new MnSapCommResponse();
        MnSapCommSDO mnSapCommSDO = new MnSapCommSDO();
        try {
            Map<String, String> header = HttpTokenUtil.generateHttpHeader();
            String urlPrefix = OtsApplicationAdapterSdkConstants.urlPrefix();
            String url = urlPrefix + "api/mn_bc_link/sap021Callback";
            JSONObject message = JSON.parseObject(callBackRequest.getMessage());
            message.put("key", message.getJSONObject("MessageHeader").getString("MESSAGEID"));
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("message", message.toJSONString());
            log.info("MnSapIf021V2ServiceImpl sap021Callback call bc link interface,url:{}, header:{}, " + "param:{}",
                url, header, jsonObject.toJSONString());
            ResponseEntity<com.alibaba.cirtus.ots.application.adapter.ability.model.Result> post = httpUtils.post(url,
                header, jsonObject, com.alibaba.cirtus.ots.application.adapter.ability.model.Result.class);
            Result result = post.getBody();
            log.info("MnSapIf021V2ServiceImpl sap021Callback result:{}", JSON.toJSONString(result));
            if (result.isSuccess()) {
                mnSapCommSDO.setStatus(MnSAPStatus.SUCCESS.getCode());
            } else {
                mnSapCommSDO.setStatus(MnSAPStatus.ERROR.getCode());
                mnSapCommSDO.setErrCode(result.getErrorCode());
                mnSapCommSDO.setMessage(result.getMessage());
            }
        } catch (Exception e) {
            log.error("MnSapIf021V2ServiceImpl callBack error", e);
            mnSapCommSDO.setStatus(MnSAPStatus.ERROR.getCode());
            mnSapCommSDO.setErrCode(OtwConstant.ORDER_001_01_01_001);
            mnSapCommSDO.setMessage(MnSAPStatus.ERROR.getMsg());
        }
        mnSapCommResponse.setData(Lists.newArrayList(mnSapCommSDO));
        return mnSapCommResponse;
    }
}
